package com.becca.cake.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.dao.system.PostDAO;
import com.becca.cake.model.system.MenuTree;
import com.becca.cake.model.system.Node;
import com.becca.cake.model.system.PostDO;
import com.becca.cake.service.system.IUserPostService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.PMSConstants;

/**
 * 岗位管理Service实现类
 * @author hejinguo
 * @version $Id: UserPostServiceImpl.java, v 0.1 2013-12-17 下午12:28:03
 */
@Service
public class UserPostServiceImpl implements IUserPostService {
    @Autowired
    private PostDAO postDAO;

    /**
     * 设置缓存
     */
    @Override
    @Cacheable(key = "'selectPostTree'", value = "monmondecakeCache")
    public String selectPostTree() throws Exception {
        //岗位树顶级ID
        Long id = Long.valueOf(PMSConstants.POST_ROOT);
        //查询岗位树
        List<Node> listNode = this.postDAO.selectPostTree(id);
        //根据ID查询当前功能
        Node node = this.postDAO.selectPostById(id);
        //封装菜单树json
        MenuTree tree = new MenuTree();
        tree.getTree(listNode, node, false);
        return tree.modifyStr(tree.returnStr.toString());
    }

    @Override
    public String selectPostMsgById(String postId) throws Exception {
        PostDO postDO = this.postDAO.selectPostMsgById(postId);
        return JsonUtil.writeObjectFormJSON(postDO);
    }

    /**
     * 删除缓存
     */
    @Override
    @CacheEvict(key = "'selectPostTree'", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertPost(PostDO postDO) throws Exception {
        this.postDAO.insertPost(postDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updatePostById(PostDO postDO) throws Exception {
        this.postDAO.updatePostById(postDO);
    }
}
